//
//  ZCInvolvedVC.m
//  zctx
//
//  Created by apple on 15/10/12.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCInvolvedVC.h"
#import "ZCSearchVC.h"

#import "ZCInvolvedModel.h"
#import "ZCInvolvedCell.h"
#import "ZCInvolvedRaiseCell.h"
#import "ZCInvolvedDetailsVC.h"
#import "ZCRaiseVC.h"

#import "ZCLoginVC.h"
@interface ZCInvolvedVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *dataArray;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,assign)NSInteger offset;
@end

@implementation ZCInvolvedVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCInvolvedVC"];
    [MTA trackPageViewBegin:@"ZCInvolvedVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCInvolvedVC"];
    [MTA trackPageViewEnd:@"ZCInvolvedVC"];
}
-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestSeeResultdata];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [self.navigationItem setTitle:@"众测项目"];
    [self initnavegation];
    _offset = 0;
    self.view.backgroundColor = [UIColor whiteColor];

    [self initTableView];
    dataArray = [NSMutableArray arrayWithCapacity:0];
    if ([theAppdelegate.tabbarIndex isEqualToString:@"2"]) {
        NSLog(@"1");
        dataArray = [self.pushArray mutableCopy];
    }else{
        [self requestSeeResultdata]; //请求数据
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInvolvedCell" bundle:nil] forCellReuseIdentifier:@"involvedcell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInvolvedRaiseCell" bundle:nil] forCellReuseIdentifier:@"onecell"];
//    self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}

-(void)loadMoreData{
    _offset = _offset +1;
    [self requestSeeResultdata];
}
#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
   
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 搜索
-(void)searchButtonPressed{
    ZCSearchVC *vc = [[ZCSearchVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark 发起众筹
-(void)RaiseButtonPressed{
    if (UserModel.password.length>1) {
        ZCRaiseVC *raise = [[ZCRaiseVC alloc]init];
        raise.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:raise animated:YES];
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

#pragma mark 请求众筹数据
-(void)requestSeeResultdata{
    if (_offset ==0) {
        [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    }
    [NetworkManager requestFoundingGetAllWithType:@"1" mid:UserModel.mid password:UserModel.password mtype:@"0" pagecount:[NSString stringWithFormat:@"%ld",(long)_offset] keywords:@"" success:^(NSDictionary *array) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([[array objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            NSArray *sarray = (NSArray *)[array objectForKey:@"content"];
            if (sarray.count>0) {
                [dataArray addObjectsFromArray:sarray];
                [self.tableView reloadData];
            }
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        
        if (([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0)||(error.code == -1001)) {
            self.bannerView.hidden = NO;
        }
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
}
#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (dataArray.count>0) {
        return 1+dataArray.count;
    }else{
        return 0;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        static NSString *cellid =@"onecell";
        ZCInvolvedRaiseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[ZCInvolvedRaiseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        return cell;
    }else{
        static NSString *cellid =@"involvedcell";
        ZCInvolvedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[ZCInvolvedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        ZCInvolvedModel *moel =[ZCInvolvedModel modelObjectWithDictionary:dataArray[indexPath.section-1]];
        cell.mainModel = moel;
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        [self RaiseButtonPressed];
    }else{
        NSString *mid =[NSString stringWithFormat:@"%@",[dataArray[indexPath.section-1] objectForKey:@"id"]];
        ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
        detailVC.pid = mid;
        detailVC.stateInt = 1;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    // 0 审核中 1众筹中 2 抽样中 3完成 4 异常
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 65;
    }else{
        return 220;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.0000001;
    }else if(section==1){
        return 44;
    }else{
        return 10;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]init];
    if(section==1){
        UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        headView.backgroundColor = [UIColor whiteColor];
    
        UIView *newView = [UIView new];
        [headView addSubview:newView];
        [newView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headView.top).with.offset(0);
            make.left.equalTo(headView).with.offset(0);
            make.right.equalTo(headView).with.offset(0);
            make.bottom.equalTo(headView.bottom).with.offset(0);
        }];
        
        UIImageView *leftImageView = [UIImageView new];
        [newView addSubview:leftImageView];
        [leftImageView setImage:[UIImage imageNamed:@"main_left.png"]];
        [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(newView.centerY).with.offset(0);
            make.left.equalTo(newView).with.offset(15);
            make.width.mas_equalTo(@4);
            make.height.equalTo(newView.height).with.offset(-20);
        }];
        
        UILabel *leftlable = [UILabel new];
        [newView addSubview:leftlable];
        leftlable.textColor = [UIColor colorWithHexString:@"#03ab9e"];
        leftlable.font = [UIFont systemFontOfSize:16];
        [leftlable setText:@"众测中的项目"];
        [leftlable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(newView.mas_centerY);
            make.left.equalTo(leftImageView.right).with.offset(5);
            make.bottom.equalTo(newView).with.offset(0);
            make.width.mas_equalTo(@150);
        }];
        return headView;
    }else{
        return headView;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
